Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ScaleDiscrete$map() compatible with vctrs-palettes #6118

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

teunbrand
Copy link
Collaborator

@teunbrand teunbrand commented Sep 20, 2024

This PR aims to fix #6117.

Briefly, ScaleDiscrete$map() has been made vctrs-aware and now works with 2D structures too.

A demonstration:

devtools::load_all("~/packages/ggplot2")
#> ℹ Loading ggplot2

rcrd <- vctrs::new_rcrd(list(foo = LETTERS[1:3], bar = 1:3), class = "foo")

# Because `alpha()` casts to character, we can show the 'rcrd' palette works
as.character.foo <- function(x) {
  x <- vec_match(x, rcrd)
  c("dodgerblue", "orchid", "limegreen")[x]
}

# For demonstration purposes
format.foo <- function(x, ...) format(vec_proxy(x), ...)

p <- ggplot(mpg, aes(displ, hwy, colour = drv)) +
  geom_point() +
  scale_colour_manual(
    values = rcrd,
    na.value = NA
  )
p

head(layer_data(p)$colour)
#> <foo[6]>
#>      
#> 1 B 2
#> 2 B 2
#> 3 B 2
#> 4 B 2
#> 5 B 2
#> 6 B 2

Created on 2024-09-20 with reprex v2.1.1

Comment on lines +979 to +982
if (obj_is_list(pal) && !obj_is_list(na_value)) {
# We prevent a casting error that occurs when mapping grid patterns
na_value <- list(na_value)
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't love this, but I also don't know how else to solve this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: vctrs-friendly scale mapping
1 participant